package com.sxkf.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.template.entity.Post;
import com.sxkf.template.entity.PostCollect;
import com.sxkf.template.entity.PostLike;
import com.sxkf.template.entity.PostNotice;
import com.sxkf.template.mapper.PostCollectMapper;
import com.sxkf.template.mapper.PostMapper;
import com.sxkf.template.service.PostCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.CheckEmptyUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 帖子收藏 服务实现类
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-12-17 15:24:12
 */
@Service
public class PostCollectServiceImpl extends ServiceImpl<PostCollectMapper, PostCollect> implements PostCollectService {


    @Resource
    private PostMapper postMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(PostCollect postCollect) {
        PostCollect one = getOne(new LambdaQueryWrapper<PostCollect>()
                .eq(PostCollect::getPostId, postCollect.getPostId())
                .eq(PostCollect::getUserId, postCollect.getUserId())
        );
        //如果已有点赞记录则取消点赞
        if (one != null){
            removeById(one);
            postMapper.update(null,new LambdaUpdateWrapper<Post>()
                    .setSql("collect_count = collect_count - 1")
                    .eq(Post::getId, postCollect.getPostId()));
            return false;
        }

        //如果未点赞则进行点赞
        save(postCollect);

        //帖子点赞量+1
        postMapper.update(null,new LambdaUpdateWrapper<Post>()
                .setSql("collect_count = collect_count + 1")
                .eq(Post::getId, postCollect.getPostId()));

//        //添加消息通知
//        PostNotice postNotice = new PostNotice();
//        postNotice.setPostId(postCollect.getPostId());
//        postNotice.setUserId(postCollect.getUserId());
//        postNotice.setType("1");
//        postNotice.setState("0");
//        String pictureJson = postMapper.selectById(postCollect.getPostId()).getPictureJson();
//        if (CheckEmptyUtil.isNotEmpty(pictureJson)){
//            String[] pics = pictureJson.split(",");
//            postNotice.setPostPicture(pics[0]);
////            List<String> pics = JSON.parseArray(pictureJson, String.class);
////            if (!pics.isEmpty()){
////                postNotice.setPostPicture(pics.get(0));
////            }
//        }
//        postNoticeMapper.insert(postNotice);
        return true;
    }

    @Override
    public Boolean isAdd(String userId, String postId) {

        PostCollect one = getOne(new LambdaQueryWrapper<PostCollect>()
                .eq(PostCollect::getPostId, postId)
                .eq(PostCollect::getUserId, userId)
        );


        return !CheckEmptyUtil.isEmpty(one);

    }

    @Override
    public Page<Post> pageByType(Page<Post> page, String userId, String type) {
        return baseMapper.pageByType(page,userId,type);
    }
}
